﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Smart_Archives_Api.DTO;
using Smart_Archives_Api.Entity;

namespace Smart_Archives_Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [EnableCors("AllowAllOrigin")]
    public class DeviceController : ControllerBase
    {
        private SaContext saContext;

        public DeviceController(SaContext saContext)
        {
            this.saContext = saContext;
        }


        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <param name="status">传-1 获取所有设备  传0获取所有被禁用设备  传1获取所有在用设备</param>
        /// <returns></returns>
        [HttpGet("GetDeviceList/{status}")]
        public Response<dynamic> GetDeviceList(int status)
        {
            var response = new Response<dynamic>();
            List<SecurityDevice> deviceList = new List<SecurityDevice>();
            if (status != -1)
            {
                deviceList = saContext.SecurityDevice.Where(a => a.Status == status).OrderBy(a => a.CreateTime)
                    .ToList();
            }
            else
            {
                deviceList = saContext.SecurityDevice.OrderBy(a => a.CreateTime).ToList();
            }

            response.Code = 1;
            response.Msg = "succeed";
            response.Data = deviceList;
            return response;
        }

        /// <summary>
        /// 根据id获取设备信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetDevice/{id}")]
        public Response<dynamic> UpdateDevice(int id)
        {
            var response = new Response<dynamic>();
            var model = saContext.SecurityDevice.Find(id);
            //saContext.SaveChanges();
            response.Code = 1;
            response.Msg = "succeed";
            response.Data = model;
            return response;
        }


        /// <summary>
        /// 根据二级分类id获取设备信息列表
        /// </summary>
        /// <param name="id">二级分类id</param>
        /// <returns></returns>
        [HttpGet("GetDeviceListByCategory/{id}")]
        public Response<dynamic> GetDeviceListByCategory(int id)
        {
            var response = new Response<dynamic>();
            var model = saContext.SecurityDevice.Where(a => a.SecurityCategoryId == id && a.Status == 1).ToList();
            //saContext.SaveChanges();
            response.Code = 1;
            response.Msg = "succeed";
            response.Data = model;
            return response;
        }
        /// <summary>
        /// 更新设备信息
        /// </summary>
        /// <param name="securityDevice"></param>
        /// <returns></returns>
        [HttpPost("UpdateDevice")]
        public Response<dynamic> UpdateDevice(SecurityDevice securityDevice)
        {
            var response = new Response<dynamic>();
            saContext.SecurityDevice.Update(securityDevice);
            saContext.SaveChanges();
            response.Code = 1;
            response.Msg = "succeed";
            return response;
        }

        /// <summary>
        /// 逻辑删除设备
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("DeleteDevice")]
        public Response<dynamic> DeleteDevice(int id)
        {
            var response = new Response<dynamic>();
            var model = saContext.SecurityDevice.Find(id);
            model.Status = 0;
            saContext.SaveChanges();
            response.Code = 1;
            response.Msg = "succeed";
            return response;
        }

        /// <summary>
        /// 根据id获取设备分类
        /// </summary>
        /// <param name="id">传-1获取所有一级分类 传0获取所有分类 传一级分类id获取改一级分类下所有二级分类</param>
        /// <returns></returns>
        [HttpGet("GetDeviceCategories/{id}")]
        public Response<dynamic> GetDeviceCategories(int id)
        {
            var response = new Response<dynamic> { Code = 1, Msg = "ok" };
            var parentCategories =
                saContext.SecurityDeviceCategory.Where(a => a.ParentId == 0 && a.Status == 1).ToList();
            if (parentCategories.Count == 0)
            {
                var list = new List<SecurityDeviceCategory>
                {
                    new SecurityDeviceCategory
                    {
                        CreateTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        Name = "摄像机",
                        Status = 1,
                        ParentId = 0,
                        Type = 0
                    },
                    new SecurityDeviceCategory
                    {
                        CreateTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        Name = "抓拍机",
                        Status = 1,
                        ParentId = 0,
                        Type = 1
                    },
                    new SecurityDeviceCategory
                    {
                        CreateTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        Name = "警戒机",
                        Status = 1,
                        ParentId = 0,
                        Type = 2
                    },
                    new SecurityDeviceCategory
                    {
                        CreateTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        Name = "门禁机",
                        Status = 1,
                        ParentId = 0,
                        Type = 3
                    }
                };
                saContext.SecurityDeviceCategory.AddRange(list);
            }
            List<SecurityDeviceCategory> data = new List<SecurityDeviceCategory>();
            if (id <= -1)
            {
                data = saContext.SecurityDeviceCategory.Where(a => a.Status == 1 && a.ParentId == 0).ToList();
            }
            else if (id == 0)
            {
                data = saContext.SecurityDeviceCategory.ToList();
            }
            else
            {
                data = saContext.SecurityDeviceCategory.Where(a => a.ParentId == id && a.Status == 1).ToList();
            }

            data = data.OrderBy(a => a.CreateTime).ToList();
            response.Data = data;
            return response;
        }

        /// <summary>
        /// 更新二级分类信息
        /// </summary>
        /// <param name="securityDeviceCategory"></param>
        /// <returns></returns>
        [HttpPost("UpdateCategory")]
        public Response<dynamic> UpdateCategory(SecurityDeviceCategory securityDeviceCategory)
        {
            var response = new Response<dynamic>();
            response.Code = 1;
            response.Msg = "ok";
            if (saContext.SecurityDeviceCategory.FirstOrDefault(a => a.Id == securityDeviceCategory.Id)?.ParentId == 0)
            {
                response.Code = 0;
                response.Msg = "一级目录无法更改";
                return response;
            }

            if (securityDeviceCategory.ParentId == 0)
            {
                response.Code = 0;
                response.Msg = "无法更改为一级目录";
                return response;
            }

            saContext.SecurityDeviceCategory.Update(securityDeviceCategory);
            saContext.SaveChanges();
            return response;
        }


        /// <summary>
        /// 新增二级分类信息
        /// </summary>
        /// <param name="securityDeviceCategory"></param>
        /// <returns></returns>
        [HttpPost("InsertCategory")]
        public Response<dynamic> InsertCategory(SecurityDeviceCategory securityDeviceCategory)
        {
            var response = new Response<dynamic>();
            response.Code = 1;
            response.Msg = "ok";

            if (securityDeviceCategory.ParentId == 0)
            {
                response.Code = 0;
                response.Msg = "无法新增一级目录";
                return response;
            }

            securityDeviceCategory.Id = 0;
            saContext.SecurityDeviceCategory.Add(securityDeviceCategory);
            saContext.SaveChanges();
            return response;
        }

        /// <summary>
        /// 逻辑删除二级分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("DeleteCategory")]
        public Response<dynamic> DeleteCategory(int id)
        {
            var response = new Response<dynamic>();
            response.Code = 1;
            response.Msg = "ok";
            var model = saContext.SecurityDeviceCategory.Find(id);
            if (model.ParentId == 0)
            {
                response.Code = 0;
                response.Msg = "一级节点无法删除";
                return response;
            }

            model.Status = 0;
            saContext.SaveChanges();
            return response;
        }

        /// <summary>
        /// 获取事件记录  0)移动侦测 1)门禁 2)rfid
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("GetDeviceEventList")]
        public Response<DeviceEventRespDTO> GetDeviceEventList(DeviceEventReqDTO dto)
        {
            var response = new Response<DeviceEventRespDTO>();
            response.Code = 1;
            response.Msg = "ok";
            DeviceEventRespDTO respDto = new DeviceEventRespDTO();
            var list = saContext.TMessageReocrd.OrderByDescending(a => a.Datetime).ToList();
            list = list.Where(a => a.Datetime.DayOfYear <= dto.EndTime.DayOfYear &&
                          a.Datetime.DayOfYear >= dto.StartTime.DayOfYear).ToList();
            respDto.TotalCount = list.Count;
            list = list.Skip(dto.PageSize * (dto.PageIndex - 1)).Take(dto.PageSize).ToList();
            respDto.Records = list;

            response.Data = respDto;
            return response;
        }
    }
}